//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)

#include "CustomerBridge.h"


TCustomerBridge :: TCustomerBridge( TDatabase * database)
{
   sequence = new TSequence( database);     

   qryGetCheckings = new TQuery( NULL);
   qryGetCheckings->DatabaseName = database->DatabaseName;
   BuildGetCheckingsQuery();
   
   qryAdd = new TQuery( NULL);
   qryAdd->DatabaseName = database->DatabaseName;
   qryAddChecking = new TQuery( NULL);
   qryAddChecking->DatabaseName = database->DatabaseName;
   BuildAddQuery();
   
   qryDelete = new TQuery( NULL);
   qryDelete->DatabaseName = database->DatabaseName;
   qryDeleteChecking = new TQuery( NULL);
   qryDeleteChecking->DatabaseName = database->DatabaseName;
   BuildDeleteQuery();
}

TCustomerBridge :: ~TCustomerBridge()
{
   delete qryAdd;
   delete qryAddChecking;
   delete qryDelete;
   delete qryDeleteChecking;
   delete sequence;
}

void TCustomerBridge :: BuildGetCheckingsQuery()
{
   qryGetCheckings->SQL->Add( "SELECT * FROM CHECKINGS WHERE ID = :prmID");
}

void TCustomerBridge :: BuildAddQuery()
{
   qryAdd->SQL->Add( "INSERT INTO CUSTOMERS VALUES( :prmID, :prmCustomerID, :prmName, :prmLastName, :prmPhone, :prmAddress, :prmEmail)");
   qryAddChecking->SQL->Add( "INSERT INTO CHECKINGS VALUES( :prmID, :prmLeft, :prmRight, :prmDate, :prmRemark)");
}

void TCustomerBridge :: BuildDeleteQuery()
{
   qryDelete->SQL->Add( "DELETE FROM CUSTOMERS WHERE ID = :prmID");
   qryDeleteChecking->SQL->Add( "DELETE FROM CHECKINGS WHERE ID = :prmID");
}

__int64 TCustomerBridge :: GetLastID()
{
   return sequence->GetLastValue( "CUSTOMERS");
}

__int64 TCustomerBridge :: GetNextID()
{
   return sequence->GetNextValue( "CUSTOMERS");
}

void TCustomerBridge :: GetCheckings( __int64 id, TStrings * checkings)
{
   AnsiString left;
   AnsiString right;
   AnsiString remark;
   TDateTime date;

   qryGetCheckings->ParamByName( "prmID")->AsInteger = id;
   qryGetCheckings->Open();

   try
   {
      for( int i = 0; i < checkings->Count; i++)
         delete checkings->Objects[ i];

      checkings->Clear();

      qryGetCheckings->First();

      while ( !qryGetCheckings->Eof)
      {
         left   = qryGetCheckings->FieldByName( "LEFT")->AsString;
         right  = qryGetCheckings->FieldByName( "RIGHT")->AsString;
         remark = qryGetCheckings->FieldByName( "REMARK")->AsString;
         date   = qryGetCheckings->FieldByName( "DATE")->AsDateTime;

         TChecking * checking = new TChecking( left, right, remark, date);

         checkings->AddObject( IntToStr( id), checking);
         qryGetCheckings->Next();
      }
   }
   __finally
   {
      qryGetCheckings->Close();
   }
}

void TCustomerBridge :: Add( __int64 id, AnsiString customerID, AnsiString name, AnsiString lastName, AnsiString phone, AnsiString address, AnsiString email, TStrings * checkings)
{
   int i;
   TChecking * checking;

   qryAdd->ParamByName( "prmID")->AsInteger = id;
   qryAdd->ParamByName( "prmCustomerID")->AsString = customerID;
   qryAdd->ParamByName( "prmName")->AsString = name;
   qryAdd->ParamByName( "prmLastName")->AsString = lastName;
   qryAdd->ParamByName( "prmPhone")->AsString = phone;
   qryAdd->ParamByName( "prmAddress")->AsString = address;
   qryAdd->ParamByName( "prmEmail")->AsString = email;
   qryAdd->ExecSQL();

   qryAddChecking->ParamByName( "prmID")->AsInteger = id;

   for( i = 0; i < checkings->Count; i++)
   {
      checking = ( TChecking *)checkings->Objects[ i];
      qryAddChecking->ParamByName( "prmLeft")->AsString = checking->left;
      qryAddChecking->ParamByName( "prmRight")->AsString = checking->right;

      if (( double)checking->date == 0)
         qryAddChecking->ParamByName( "prmDate")->AsDateTime = NULL;
      else
         qryAddChecking->ParamByName( "prmDate")->AsDateTime = checking->date;

      qryAddChecking->ParamByName( "prmRemark")->AsString = checking->remark;

      qryAddChecking->ExecSQL();
   }
}

void TCustomerBridge :: Update( __int64 id, AnsiString customerID, AnsiString name, AnsiString lastName, AnsiString phone, AnsiString address, AnsiString email, TStrings * checkings)
{
   Delete( id);
   Add( id, customerID, name, lastName, phone, address, email, checkings);
}

void TCustomerBridge :: Delete( __int64 id)
{
   qryDelete->ParamByName( "prmID")->AsInteger = id;
   qryDelete->ExecSQL();
   qryDeleteChecking->ParamByName( "prmID")->AsInteger = id;
   qryDeleteChecking->ExecSQL();
}

//---------------------------------------------------------------------------

TChecking :: TChecking( AnsiString left, AnsiString right, AnsiString remark, TDateTime date)
{
   this->left   = left;
   this->right  = right;
   this->remark = remark;
   this->date   = date;
}

